package com.xiaoming.controller;

import com.xiaoming.base.ResponseBase;
import com.xiaoming.constants.Constants;
import com.xiaoming.entity.User;
import com.xiaoming.feign.UserServiceFegin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * 注册控制器
 *
 * @author xiaoming
 * @Date 2020/3/12
 * @blame Android Team
 */
@Controller
public class RegisterController {

    @Autowired
    private UserServiceFegin userServiceFegin;

    private static final String LOGIN = "login";
    private static final String REGISTER = "register";

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String register() {
        return REGISTER;
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(User user, HttpServletRequest reqest) {
        ResponseBase registerUser = userServiceFegin.registerUser(user);
        if (!registerUser.getCode().equals(Constants.HTTP_RES_CODE_200)) {
            reqest.setAttribute("error", registerUser.getMessage());
            return REGISTER;
        }
        // 2.注册成功，跳转到登录页面
        return LOGIN;
    }

}
